package rursu.business;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class Settings {

	private static final String LOGGER_TAG = Settings.class.getCanonicalName();
	
	public final static String ORDER_KEY = "order_by";
	public final static String NUM_OF_ITEMS_KEY = "update_freq_option";
	
	private final String DEFAULT_ORDER_VALUE = "release";
	private final int DEFAULT_UPDATE_FREQ_VALUE = 1;
	
	public final String MOVIE_APPLICATION_PREFERENCES = "MOVIE_APPLICATION_PREFERENCES";
	private Context context;
	private Map<String, Object> settins = new HashMap<String, Object>();
	
	private boolean isChanged = false;
	
	public Settings(Context context) {
		super();
		this.context = context;
	}
	
	public void updateSettings() {
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		
		int numOfItemsNew = Integer.parseInt(sharedPreferences.getString(NUM_OF_ITEMS_KEY, "0"));
		String orderNew = sharedPreferences.getString(ORDER_KEY, "unknown");
	
		Object numOfItemsKeyObject = settins.get(NUM_OF_ITEMS_KEY);
		Object orderOldObject = settins.get(ORDER_KEY);
		
		if (numOfItemsKeyObject != null && orderOldObject != null) {
			
			int numOfItemsOld = ((Integer)( numOfItemsKeyObject ));
			String orderOld = ((String)( orderOldObject ));
			
			if ( !orderOld.equals(orderNew) || !(numOfItemsOld == numOfItemsNew)) {
				isChanged = true;
				
				Log.d("SETTINGS", "Settings were changed");
				Log.d("SETTINGS", "order: oldValue = " + orderOld + "      newValue = " + orderNew);
				Log.d("SETTINGS", "numOfItems: oldValue = " + numOfItemsOld + "      newValue = " + numOfItemsNew);
				
			} else {
				isChanged = false;
				
				Log.d("SETTINGS", "Settings weren't changed");
			}
		} else {
			isChanged = true;
		}
		
		if (isChanged == true) {
			settins.put(NUM_OF_ITEMS_KEY, numOfItemsNew);
			settins.put(ORDER_KEY, orderNew);
		}
		
	}
	
	public void initPreferences() {
		SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor = sharedPreferences.edit();
		
		editor.putString(ORDER_KEY, DEFAULT_ORDER_VALUE);
		editor.putInt(NUM_OF_ITEMS_KEY, DEFAULT_UPDATE_FREQ_VALUE);
		
		editor.commit();
		
		settins.put(ORDER_KEY, DEFAULT_ORDER_VALUE);
		settins.put(NUM_OF_ITEMS_KEY, DEFAULT_UPDATE_FREQ_VALUE);
		
	}

	public boolean isChanged() {
		return isChanged;
	}	
	
	public void setIsChanged(boolean key) {
		isChanged = key;
	}
	
	public Object getSetting(String key) { 
		
		verifyReady();
		
		return settins.get(key);
	}

	private void verifyReady() {

		
		Object numOfItemsKeyObject = settins.get(NUM_OF_ITEMS_KEY);
		Object orderOldObject = settins.get(ORDER_KEY);
		
		if (numOfItemsKeyObject == null || orderOldObject == null) {
			
			Log.v(LOGGER_TAG, "Initialization of settings");
			
			SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
			
			int numOfItemsNew = Integer.parseInt(sharedPreferences.getString(NUM_OF_ITEMS_KEY, "10"));
			String orderNew = sharedPreferences.getString(ORDER_KEY, "release");
			
			settins.put(NUM_OF_ITEMS_KEY, numOfItemsNew);
			settins.put(ORDER_KEY, orderNew);
		}		
	}
}
